import { Pipe, PipeTransform } from '@angular/core';
import { ZoneService } from 'app/modules/zone/services/zone.service';
import { Observable, Observer } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Pipe({
	name: 'zoneInfo',
})
export class ZoneInfoPipe implements PipeTransform {
	zones$
	zoneslist = []
	constructor(private zoneService: ZoneService,public http:HttpClient) {
	    this.batch_areas()
		this.zones$=new Observable((observer: Observer<any>) => {
			this.batch_areas().subscribe( (zones:any)=>{		
				this.zonefor(zones.message)	
				observer.next(this.zoneslist);
				observer.complete();
			  })
		}).toPromise();
	}

	batch_areas() {
		return new Observable((observer: Observer<any>) => {
		  this.http.get('api/admin/batch_areas').subscribe((res) => {
	
			observer.next(res);
			observer.complete();
		  });
		})
	  }
	
	zonefor(e){
		e.forEach(element => {
			this.zoneslist.push(element)
			if(element.children){
				this.zonefor(element.children)
			}
		});
	}
	async transform(zid: number) {	
		const zones = await this.zones$;
		return zones.find(zone => zone.area_id == zid );
	}
}


